API:
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2
3 /*
4 * Created on July 7, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to enumerate all available pages.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'al');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
48 }
49
50 private function run($resultPageSet = null) {
51
52 $db = $this->getDB();
53 $params = $this->extractRequestParams();
54 $this->debugPrint($params);
55
56 $prop = array_flip($params['prop']);
57 $fld_ids = isset($prop['ids']);
58 $fld_title = isset($prop['title']);
59
60 if ($params['unique']) {
61 if (!is_null($resultPageSet))
62 $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
63 if ($fld_ids)
64 $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
65 $this->addOption('DISTINCT');
66 }
67
68 $this->addTables('pagelinks');
69 $this->addWhereFld('pl_namespace', $params['namespace']);
70
71 if (!is_null($params['from']))
72 $this->addWhere('pl_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
73 if (isset ($params['prefix']))
74 $this->addWhere("pl_title LIKE '" . $db->strencode(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
75
76 if (is_null($resultPageSet)) {
77 $this->addFields(array (
78 'pl_namespace',
79 'pl_title'
80 ));
81 $this->addFieldsIf('pl_from', $fld_ids);
82 } else {
83 $this->addFields('pl_from');
84 $pageids = array();
85 }
86
87 $this->addOption('USE INDEX', 'pl_namespace');
88 $limit = $params['limit'];
89 $this->addOption('LIMIT', $limit+1);
90 $this->addOption('ORDER BY', 'pl_namespace, pl_title');
91
92 $res = $this->select(__METHOD__);
93
94 $data = array ();
95 $count = 0;
96 while ($row = $db->fetchObject($res)) {
97 if (++ $count > $limit) {
98 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
99 // TODO: Security issue - if the user has no right to view next title, it will still be shown
100 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->pl_title));
101 break;
102 }
103
104 if (is_null($resultPageSet)) {
105 $title = Title :: makeTitle($row->pl_namespace, $row->pl_title);
106 if ($title->userCanRead()) {
107 $vals = array();
108 if ($fld_ids)
109 $vals['fromid'] = intval($row->pl_from);
110 if ($fld_title) {
111 $vals['ns'] = intval($title->getNamespace());
112 $vals['title'] = $title->getPrefixedText();
113 }
114 $data[] = $vals;
115 }
116 } else {
117 $pageids[] = $row->pl_from;
118 }
119 }
120 $db->freeResult($res);
121
122 if (is_null($resultPageSet)) {
123 $result = $this->getResult();
124 $result->setIndexedTagName($data, 'l');
125 $result->addValue('query', $this->getModuleName(), $data);
126 } else {
127 $resultPageSet->populateFromPageIDs($pageids);
128 }
129 }
130
131 protected function getAllowedParams() {
132 return array (
133 'from' => null,
134 'prefix' => null,
135 'unique' => false,
136 'prop' => array (
137 ApiBase :: PARAM_ISMULTI => true,
138 ApiBase :: PARAM_DFLT => 'title',
139 ApiBase :: PARAM_TYPE => array (
140 'ids',
141 'title'
142 )
143 ),
144 'namespace' => array (
145 ApiBase :: PARAM_DFLT => 0,
146 ApiBase :: PARAM_TYPE => 'namespace'
147 ),
148 'limit' => array (
149 ApiBase :: PARAM_DFLT => 10,
150 ApiBase :: PARAM_TYPE => 'limit',
151 ApiBase :: PARAM_MIN => 1,
152 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
153 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
154 )
155 );
156 }
157
158 protected function getParamDescription() {
159 return array (
160 'from' => 'The page title to start enumerating from.',
161 'prefix' => 'Search for all page titles that begin with this value.',
162 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
163 'namespace' => 'The namespace to enumerate.',
164 'limit' => 'How many total links to return.'
165 );
166 }
167
168 protected function getDescription() {
169 return 'Enumerate all pages sequentially in a given namespace';
170 }
171
172 protected function getExamples() {
173 return array (
174 'api.php?action=query&list=alllinks&alunique&alfrom=B',
175 );
176 }
177
178 public function getVersion() {
179 return __CLASS__ . ': $Id$';
180 }
181 }